package hotel.action;

import hotel.data.MD5Creator;
import hotel.form.ChangePasswordForm;
import hotel.model.IUser;
import hotel.service.ServiceManager;
import hotel.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
 * This class change user password
 * @author Alex
 *
 */
public class ChangePasswordAction extends Action 
{
	private static final Logger logger = Logger.getLogger(ChangePasswordAction.class);
	private static final String CHANGEPASSWORDACTION_ERROR = "Error in ChangePasswordAction:";
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response) throws Exception 
	 {
		 String mapper = ForwardActionTo.ERROR;
		 try
		 {
		     ChangePasswordForm changePasswordForm = (ChangePasswordForm)form;
		     HttpSession session = request.getSession(true);
		     ActionMessages errors = new ActionMessages();
		     IUser user = null;
			 UserService userService = null;
	
			 if (changePasswordForm != null) 
		     {		 
				 
				changePasswordForm.setOldPassword(MD5Creator.MD5(changePasswordForm.getOldPassword()));
				changePasswordForm.setNewPassword(MD5Creator.MD5(changePasswordForm.getNewPassword()));
				changePasswordForm.setAgainNewPassword(MD5Creator.MD5(changePasswordForm.getAgainNewPassword()));
				
				userService = ServiceManager.getDefault().getUserService();
				user = (IUser)session.getAttribute("user");
				
				if (!changePasswordForm.getNewPassword().equals(changePasswordForm.getAgainNewPassword()))
				{
					errors.add("differrentPasswords", new ActionMessage("error.password.different"));
	       	 	 	saveMessages(request,errors);
	       	 	 	mapper = ForwardActionTo.ERROR;
				}
				else
					if(changePasswordForm.getOldPassword().equals(changePasswordForm.getNewPassword()))
					{
						errors.add("not_differrentPasswords", new ActionMessage("error.password.not_different"));
						saveMessages(request,errors);
		       	 	 	mapper = ForwardActionTo.ERROR;
					}
					else
					 if ( user != null )
						 if ( changePasswordForm.getOldPassword().equals(user.getPassword()))
						 {
							 userService.changePassword(changePasswordForm.getNewPassword(), user.getId());
							 errors.add("changePassword_success", new ActionMessage("message.password.change.success"));
							 saveMessages(request,errors);
							 mapper = ForwardActionTo.SUCCESS;
						 }
						 else
							 if ( changePasswordForm.getOldPassword().equals("") )
					         {
					        	 errors.add("password", new ActionMessage("error.password"));
					        	 saveMessages(request,errors);
				        	 	 mapper = ForwardActionTo.ERROR;
					         }
							 else
							 {
								 errors.add("wrongPassword", new ActionMessage("error.wrongpassword"));
								 saveMessages(request,errors);
								 mapper = ForwardActionTo.ERROR;
							 }
	         }
		 }
         catch(NullPointerException e)
		 {
		 	logger.error(CHANGEPASSWORDACTION_ERROR + e);
		 }
         return mapping.findForward(mapper);
	 }

}